package identityController

import (
	"fmt"
	identityservice "identity/baseService/identityService"
	"identity/infrastructure/common/dto"
	"time"
)

var identityList = make([]dto.IdentityDto, 100)

// 身份控制器-类
type IdentityController struct {
	identityservice identityservice.IdentityService
}

func New() IdentityController {
	ic := IdentityController{}
	ic.identityservice = identityservice.Create()
	return ic
}

// 接口名称：添加身份信息
// 请求：身份信息
// 响应：身份信息+Id+code
func (ic *IdentityController) AddIdentity(name string, desc string) dto.ResultDto {
	// step init:
	var result dto.ResultDto
	var identity dto.IdentityDto

	// step 1: 记录进入信息
	identityList = append(identityList, identity)

	fmt.Println("AddIdentity is executed")

	//debug: 1模拟延时
	time.Sleep(time.Second * 2)

	// step 4：添加数据
	identity, err := ic.identityservice.AddIdentity(name, desc)
	if err != nil {
		result.Code = 10001
		result.Success = false
		result.Message = "失败，" + err.Error()
		return result
	}

	// step 5：end
	result.Code = 200
	result.Success = true
	result.Message = "成功"
	result.Data = identity
	return result
}

//     2.3 查看身份信息
func (ic *IdentityController) GetIdentity(id int64) dto.ResultDto {

	// step 1: 记录进入信息
	fmt.Println("GetIdentity is executed")
	result := dto.ResultDto{}

	//debug: 1模拟延时
	time.Sleep(time.Second * 2)

	// step 4：查询数据
	identitydto, e3 := ic.identityservice.FindIdentity(id)
	if !(e3 == nil) {
		result.Code = 500
		result.Success = false
		result.Message = "查询身份信息异常，-" + e3.Error()
		return result
	}
	// step 5：end
	result.Code = 200
	result.Success = true
	result.Data = identitydto
	result.Message = "成功"
	return result
}
